package com.comut.crud;

import com.comut.crud.ejb.interfaces.remote.ITrailerActionRemote;
import com.comut.crud.pojo.Response;
import com.comut.crud.pojo.mobile.TrailerInfo;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

/**
 * Issue: IFL-1572
 *
 * @author Vitor Borrego
 */
@Path("trailersForUserId/{userId}")
public class TrailersForUserIdResource {

    private final String jndiName = ResourceBundle.getBundle("jndi").getString("trailerBeanRemote");
    private static final Logger logger = Logger.getLogger(TrailersForUserIdResource.class.getName());

    @GET
    @Produces("application/json; charset=UTF-8")
    public Response getJson(@PathParam("userId") int userId) {
        // http://localhost:8080/CRUD-EA-web/resources/trailersForUserId/332
        Response response = new Response();

        try {
            logger.log(Level.INFO, "GET STARTED");
            javax.naming.Context ic = new InitialContext();
            ITrailerActionRemote trailerAction = (ITrailerActionRemote) ic.lookup(jndiName);
            TrailerInfo[] trailerInfoArray = trailerAction.getTrailersForUserId(userId);
            logger.log(Level.INFO, "GET DONE");
            response.setRetVal(trailerInfoArray);
            response.setSuccess(true);
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
            response.setSuccess(false);
            response.setMessageError(ex.getMessage());
        }

        return response;
    }
}
